package ace.module.oss.core.impl.service.impl;

import ace.module.oss.core.impl.dao.entity.OssApplicationConfig;
import ace.module.oss.core.impl.dao.entity.OssApplicationSupplier;
import ace.module.oss.core.impl.dao.entity.OssSupplier;
import ace.module.oss.core.impl.manager.*;
import ace.module.oss.core.impl.service.OssService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@AllArgsConstructor
@Component
public class OssServiceImpl implements OssService {

  private final OssApplicationManager ossApplicationManager;
  private final OssApplicationSupplierManager ossApplicationSupplierManager;
  private final OssApplicationConfigManager ossApplicationConfigManager;
  private final OssSupplierManager ossSupplierManager;
  private final OssManagerFactory ossManagerFactory;

  @Override
  public String upload(Long ossAppId, String key, MultipartFile uploadFile) {
    ossApplicationManager.getAndCheckById(ossAppId);
    OssApplicationConfig ossApplicationConfig =
        this.ossApplicationConfigManager.getOrSystemDefaultByOssAppIdAndKeyAndDeleteFlagIsFalse(
            ossAppId, key);
    OssApplicationSupplier ossApplicationSupplier =
        this.ossApplicationSupplierManager.getAndCheckByOssAppId(ossAppId);
    OssSupplier ossSupplier =
        this.ossSupplierManager.getAndCheckById(ossApplicationSupplier.getOssSupplierId());
    OssManager ossManager = this.ossManagerFactory.get(ossApplicationConfig, ossSupplier);
    String url = ossManager.upload(uploadFile);
    return url;
  }
}
